package com.yxw.live_vod_backend_boot.service.impl;



import lombok.extern.slf4j.Slf4j;
import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxw.live_vod_backend_boot.entity.YxLiveAgreementEntity;
import com.yxw.live_vod_backend_boot.dto.YxLiveAgreementDTO;
import com.yxw.live_vod_backend_boot.dto.YxLiveAgreementAddDTO;
import com.yxw.live_vod_backend_boot.dto.YxLiveAgreementQueryDTO;  
import com.yxw.live_vod_backend_boot.mapper.YxLiveAgreementMapper;
import com.yxw.live_vod_backend_boot.service.IYxLiveAgreementService;
import com.yxw.live_vod_backend_boot.util.RedisManager;
import com.yxw.live_vod_backend_facade.constant.LiveConstant;
import javax.annotation.Resource;
import com.yxw.live_vod_backend_facade.vo.LiveAgreementVO;

/*** 
*@author Jiaxu.Huang
*@date 2023/12/10 11:46:16
*@update 2023/12/10 11:46:16
**/
@Slf4j
@Service
public class YxLiveAgreementServiceImpl  implements IYxLiveAgreementService{
    @Resource
    private RedisManager redisManager;    
    @Autowired
    private YxLiveAgreementMapper oYxLiveAgreementMapper;
    /*** 
    * 保存
    * @param oYxLiveAgreementDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult insertYxLiveAgreement(YxLiveAgreementAddDTO oYxLiveAgreementAddDTO)
    {
        QueryWrapper<YxLiveAgreementEntity> oYxLiveAgreementQueryWrapper = new QueryWrapper();
        List<YxLiveAgreementEntity> oListYxLiveAgreement = oYxLiveAgreementMapper.selectList(oYxLiveAgreementQueryWrapper);
        YxLiveAgreementEntity oYxLiveAgreementEntity = oYxLiveAgreementAddDTO.ConverToEntity();
        oYxLiveAgreementEntity.setId(null);
        if(oListYxLiveAgreement.isEmpty())
        {
            oYxLiveAgreementEntity.setState(1);
        }
        else
        {
            if(oYxLiveAgreementEntity.getState() == 1)
            {
                for(YxLiveAgreementEntity oYxLiveAgreementEntityInner : oListYxLiveAgreement)
                {
                    if(oYxLiveAgreementEntityInner.getState() == 1)
                    {
                        UpdateWrapper<YxLiveAgreementEntity> oUpdateWrapper= new UpdateWrapper();
                        oUpdateWrapper.set("state", 0);
                        oUpdateWrapper.set("update_time", BaseDateTime.getDate());
                        oUpdateWrapper.eq("agreement_id", oYxLiveAgreementEntityInner.getId());
                        oYxLiveAgreementMapper.update(null, oUpdateWrapper);
                    }
                }
            }
        }
        oYxLiveAgreementMapper.insert(oYxLiveAgreementEntity);
        if(oYxLiveAgreementEntity.getState() == 1)
        {
            redisManager.set(LiveConstant.LiveActiveAgreementRedisKey, oYxLiveAgreementEntity.convertToLiveAgreementVO());
        }
        return BaseResult.success(oYxLiveAgreementEntity);
    }
    /*** 
    * 修改
    * @param oYxLiveAgreementDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult updateYxLiveAgreementById(YxLiveAgreementDTO oYxLiveAgreementDTO)
    {
        YxLiveAgreementEntity oYxLiveAgreementEntity = oYxLiveAgreementMapper.selectById(oYxLiveAgreementDTO.getId());
        if(null == oYxLiveAgreementEntity)
        {
            return BaseResult.failed("根据Id找不到需要修改的数据");
        }
        if(oYxLiveAgreementEntity.getState() == 1 && oYxLiveAgreementDTO.getState() != 1)
        {
            return BaseResult.failed("不能将正在生效的数据改成无效");
        }
        oYxLiveAgreementEntity.setAgreementContent(oYxLiveAgreementDTO.getAgreementContent());
        oYxLiveAgreementEntity.setUpdateTime(BaseDateTime.getDate());
        oYxLiveAgreementEntity.setUserId(oYxLiveAgreementDTO.getUserId());
        oYxLiveAgreementMapper.updateById(oYxLiveAgreementEntity);
        if(oYxLiveAgreementEntity.getState() == 1)
        {
            redisManager.set(LiveConstant.LiveActiveAgreementRedisKey, oYxLiveAgreementEntity.convertToLiveAgreementVO());
        }
        return BaseResult.success();
    }
    /*** 
    * 删除
    * @param id
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult deleteYxLiveAgreementById(Integer id)
    {
        YxLiveAgreementEntity oYxLiveAgreementEntity = oYxLiveAgreementMapper.selectById(id);
        if(null == oYxLiveAgreementEntity)
        {
            return BaseResult.failed("根据Id找不到需要删除的数据");
        }
        if(oYxLiveAgreementEntity.getState() == 1)
        {
            return BaseResult.failed("不能删除正在生效的数据");
        }
        oYxLiveAgreementMapper.deleteById(id);
        return BaseResult.success();
    }
    /*** 
    * 根据id查找
    * @param id
    * @return YxLiveAgreementEntity
    **/
    @Override
    public YxLiveAgreementEntity selectYxLiveAgreementById(Integer id)
    {
        if(ObjectUtil.isEmpty(id))
        {
            return null;
        }
        return oYxLiveAgreementMapper.selectById(id);
    }
    /*** 
    * 自定义查询
    * @param oYxLiveAgreementDTO
    * @return List<YxLiveAgreementEntity>
    **/
    @Override
    public List<YxLiveAgreementEntity> selectYxLiveAgreementList(YxLiveAgreementQueryDTO oYxLiveAgreementQueryDTO)
    {
        QueryWrapper<YxLiveAgreementEntity> oYxLiveAgreementQueryWrapper = new QueryWrapper();
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getId()),"agreement_id",oYxLiveAgreementQueryDTO.getId());
        oYxLiveAgreementQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getAgreementContent()),"agreement_content",oYxLiveAgreementQueryDTO.getAgreementContent());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getUserId()),"user_id",oYxLiveAgreementQueryDTO.getUserId());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getState()),"state",oYxLiveAgreementQueryDTO.getState());
        oYxLiveAgreementQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getStartCreateTime()),"create_time",oYxLiveAgreementQueryDTO.getStartCreateTime());
        oYxLiveAgreementQueryWrapper.le(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getEndCreateTime()),"update_time",oYxLiveAgreementQueryDTO.getEndCreateTime());
        return  oYxLiveAgreementMapper.selectList(oYxLiveAgreementQueryWrapper);
    }
    /*** 
    * 自定义统计
    * @param oYxLiveAgreementEntity
    * @return Integer
    **/
    @Override
    public Integer selectYxLiveAgreementCount(YxLiveAgreementQueryDTO oYxLiveAgreementQueryDTO)
    {
        QueryWrapper<YxLiveAgreementEntity> oYxLiveAgreementQueryWrapper = new QueryWrapper();
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getId()),"agreement_id",oYxLiveAgreementQueryDTO.getId());
        oYxLiveAgreementQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getAgreementContent()),"agreement_content",oYxLiveAgreementQueryDTO.getAgreementContent());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getUserId()),"user_id",oYxLiveAgreementQueryDTO.getUserId());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getState()),"state",oYxLiveAgreementQueryDTO.getState());
        oYxLiveAgreementQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getStartCreateTime()),"create_time",oYxLiveAgreementQueryDTO.getStartCreateTime());
        oYxLiveAgreementQueryWrapper.le(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getEndCreateTime()),"update_time",oYxLiveAgreementQueryDTO.getEndCreateTime());
        return oYxLiveAgreementMapper.selectCount(oYxLiveAgreementQueryWrapper);
    }
    /*** 
    * 自定义分页查询
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oYxLiveAgreementEntity
    * @return Page
    **/
    @Override
    public Page selectYxLiveAgreementPage(int currentPage,int pageSize,YxLiveAgreementQueryDTO oYxLiveAgreementQueryDTO)
    {        
        Page<YxLiveAgreementEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxLiveAgreementEntity> oYxLiveAgreementQueryWrapper = new QueryWrapper();
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getId()),"agreement_id",oYxLiveAgreementQueryDTO.getId());
        oYxLiveAgreementQueryWrapper.like(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getAgreementContent()),"agreement_content",oYxLiveAgreementQueryDTO.getAgreementContent());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getUserId()),"user_id",oYxLiveAgreementQueryDTO.getUserId());
        oYxLiveAgreementQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getState()),"state",oYxLiveAgreementQueryDTO.getState());
        oYxLiveAgreementQueryWrapper.ge(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getStartCreateTime()),"create_time",oYxLiveAgreementQueryDTO.getStartCreateTime());
        oYxLiveAgreementQueryWrapper.le(ObjectUtil.isNotEmpty(oYxLiveAgreementQueryDTO.getEndCreateTime()),"update_time",oYxLiveAgreementQueryDTO.getEndCreateTime());
        return oYxLiveAgreementMapper.selectPage(pageParams, oYxLiveAgreementQueryWrapper);
    }
    /*** 
    * 获取正在生效的主播协议
    * @return List<YxLiveAgreementEntity>
    **/
    @Override
    public BaseResult selectActiveAgreement()
    {
        LiveAgreementVO oLiveAgreementVO = (LiveAgreementVO)redisManager.get(LiveConstant.LiveActiveAgreementRedisKey);
        if(null == oLiveAgreementVO)
        {
            QueryWrapper<YxLiveAgreementEntity> oYxLiveAgreementQueryWrapper = new QueryWrapper();
            oYxLiveAgreementQueryWrapper.eq("state",1);
            YxLiveAgreementEntity oYxLiveAgreementEntity = oYxLiveAgreementMapper.selectOne(oYxLiveAgreementQueryWrapper);
            if(null != oYxLiveAgreementEntity)
            {
                oLiveAgreementVO = oYxLiveAgreementEntity.convertToLiveAgreementVO();
                redisManager.set(LiveConstant.LiveActiveAgreementRedisKey, oLiveAgreementVO);
            }
        }
        if(null != oLiveAgreementVO)
        {
            return BaseResult.success(oLiveAgreementVO);
        }
        else
        {
            return BaseResult.failed("还没有添加主播协议");
        }
    }
}
    

